/* **************************************************************************/
/*COPYRIGHT CCWIN-IN ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.      */
/*V8:ConvertMode=Report                                                     */
/*V8:WebEnabled=Yes                                                         */
/*V8:RunMode=Character,Windows                                              */
/*EXTRACTING DATA FROM MFG/PRO                                              */
/****************************************************************************/
/* PROCEDURE NAME        : xxcustomershippermtapi.p                         */
/* PROCEDURE TYPE        : JSON API Process                                 */
/* DESCRIPTION           : Customer shipper CIM-LOAD Process                */
/*                                                                          */     
/* INCLUDE FILES         :                                                  */
/* CALLED BY             :                                                  */
/* CALLED PROCEDURES     :                                                  */
/****************************************************************************/
/* CREATED BY            : Gaoyan                               30-Mar-24   */
/****************************************************************************/ 
/****************************************************************************/
/****************************************************************************/

{us/mf/mfdtitle.i}
{us/bbi/gplabel.i}
{us/wb/wbrp01.i}
{xxjson.i}
{xxcimload.i}


{xxapilog.i}

&SCOPED-DEFINE TimeStamp1 substring(string(year(today), "9999"),3) + string(month(today), "99") + string(day(today), "99")
&SCOPED-DEFINE TimeStamp2 substring(string(time, "HH:MM:SS"), 1, 2) + substring(string(time, "HH:MM:SS"), 4, 2) + substring(string(time, "HH:MM:SS"), 7, 2)

define input parameter vc_inputjson             as character format "x(60)" no-undo.
define input parameter vc_outputjson            as character format "x(60)" no-undo.
define input parameter vc_import                as logical  no-undo.


DEFINE VARIABLE li_access           AS    INTEGER                       NO-UNDO.
define variable l_file                       as character format "x(40)" no-undo.
define variable l_line                       as character   no-undo.
define variable lvi_row_id                   as decimal     no-undo.
define variable v_import                     as logical     init   "no"      no-undo.
define variable v_site                       like si_site   no-undo.

define variable l_error                      as character      no-undo.
define stream f_log.
define stream f_cim.
define variable v_quote               as character initial """"   no-undo.
define variable v_default             as character initial "-"    no-undo.
define variable v_space               as character initial " "    no-undo.
define variable v_log                 as character                no-undo.
define variable v_cim                 as character                no-undo.
define variable v_fline               as character                no-undo.
define variable v_errorstatus         as logical                  no-undo.
define variable v_toterrorstatus         as logical                  no-undo.
define variable v_errstring           as character                no-undo.
define variable v_status              as character                no-undo.

define variable v_entity              like si_entity              no-undo.
define variable v_ee                  as logic                    no-undo.

define variable vi_filecount          as integer                  no-undo.
define variable vi_reccount           as integer                  no-undo.
define variable vc_filename           as character                no-undo.
define variable vc_topath             as character                no-undo.
define variable v_runproagram        as character                no-undo.
define variable vc_traceid            as character                no-undo.
define variable scheduled_orders_exist like mfc_logical.

            
define temp-table ttcustomerasn no-undo
        
       field  abs_customer               like abs_mstr.abs_shipfrom
       field  abs_asn_nbr                like abs_mstr.abs_id
       field  abs_site                   like abs_mstr.abs_shipto 
       field  abs_shipto                 like abs_mstr.abs_shipto 
       field  abs_inv_movment_code       like abs_mstr.abs_shipto 
       field  abs_shipfrom               like abs_mstr.abs_shipto 
       field  abs_loc                    like   abs_mstr.abs_loc 
       field  abs_ship_date              like abs_mstr.abs_shp_date
       field  abs_arrive_date            like abs_mstr.abs_shp_date
       field  abs_due_date               like abs_mstr.abs_shp_date
       field  abs_part                   like scx_ref.scx_part
       field  abs_cust_part              like scx_ref.scx_part
       field  abs_po_nbr                 as   character
       field  abs_model_yr               as   character
       field  abs_order                  like po_nbr
       field  abs_line                   like scx_ref.scx_line
       field  abs_qty                    as  decimal
       field  abs_um                     like pt_mstr.pt_um
       field  abs_conv                   as  decimal
       field  abs_traceid                as   character
       field  abs_dataid                 as   character
       field  abs_cimfile                 as   character
       field  abs_logfile                 as   character
        .

      DEFINE DATASET dsttcustomerasn FOR ttcustomerasn.

      hdataset = dataset dsttcustomerasn:HANDLE.    


mainloop:
repeat on error undo mainloop, retry mainloop:

   /* Output and Batch Input */
   
   {us/xx/xxworkpath_e.i &outbound=YES &inbound=no &archive=YES}

assign m_outpath     = m_outpath + "asn/"
       m_archpath    = m_archpath + "asn/"
       m_successpath = m_successpath + "asn/"
       m_errorpath   = m_errorpath + "asn/"
       m_workpath    = m_workpath + "asn/".


   empty temp-table ttcustomerasn.

      /* Load item master data from JSON  */ 
      vc_outputjson = replace(vc_inputjson,".json",".run.out.json").   
      run loadjsonfile(input vc_inputjson,
                       input vc_outputjson,
                       output vl_RetOK).
      if vl_RetOK then
      do:
         find first ttcustomerasn no-error.
         assign vc_traceid = ttcustomerasn.abs_traceid.
      end.

  /* Create CIM-LOAD file and do cim-load */
  v_cim = string(vc_traceid).
  v_log = string(vc_traceid).
  
 output to /home/mfg/work/xxcustomerasnapi.log  .      
 
if vc_import and vl_RetOK then do:  

  /* create cim load format */
                
  cimloadprocess:
  do transaction on error undo cimloadprocess, leave cimloadprocess: 
  
  for each ttcustomerasn  break by ttcustomerasn.abs_asn_nbr.

   if first-of(ttcustomerasn.abs_asn_nbr) then do:

    v_cim = m_workpath + "ASN" + "_" + string(ttcustomerasn.abs_traceid) + "_" + string(ttcustomerasn.abs_asn_nbr) + "_" + {&TimeStamp1} + "_" + {&TimeStamp2}  + ".cim".
    v_log = m_workpath + "ASN" + "_" + string(ttcustomerasn.abs_traceid) + "_" + string(ttcustomerasn.abs_asn_nbr) + "_" + {&TimeStamp1} + "_" + {&TimeStamp2}  + ".log".
    
    output stream f_cim to value(v_cim).
    
       find first so_mstr where so_domain = global_domain
                            and so_nbr = ttcustomerasn.abs_order
                            no-lock no-error.
    
       run findSchedOrders
      (input global_domain,
       input so_ship,
       input ttcustomerasn.abs_shipfrom,
       input so_ship,
       output scheduled_orders_exist).

    put stream f_cim  unformatted 
            v_quote ttcustomerasn.abs_shipfrom v_quote v_space                    /*  Ship from */
            /*
            v_quote replace(ttcustomerasn.abs_asn_nbr,"-","")  v_quote v_space                    /*  ASN */
            */
            v_default v_space                                                     /* asn  auto generate number */
            v_quote so_ship  v_quote v_space                                      /*  customer */
            v_quote ttcustomerasn.abs_inv_movment_code v_quote  v_space   skip    /*  inventory movement code  */
            v_default v_space                                                     /* carrier */
            v_default v_space                                                     /* multi_entry */
            v_default v_space                                                     /* abs_shipvia */
            v_default v_space                                                     /* abs_fob */
            v_default v_space                                                     /* abs_trans_mode */
            v_quote ttcustomerasn.abs_asn_nbr v_quote v_space                     /* abs_carr_ref */
            v_default v_space                                                     /* abs_veh_ref */
            v_default v_space                                                     /* abs_pay_type */
            v_default v_space                                                     /* abs_carr_acct */
            v_default v_space                                                     /* abs_mstr.abs_format */
            v_default v_space                                                     /* cons_ship */
            v_default v_space                                                     /* abs_mstr.abs_lang */
            "N" v_space                                                           /* cmmts */
            v_default v_space  skip                                               /* abs_totalpallet */
            "."   skip.                                                           /* abs_cont_id */
     end. /* if first-of(ttcustomerasn.abs_asn_nbr) then do: */ 
     
     if scheduled_orders_exist then 
     put stream f_cim unformatted
            v_quote ttcustomerasn.abs_part v_quote v_space                        /* part */
            v_quote ttcustomerasn.abs_po_nbr v_quote v_space                      /* scx_ref.scx_po */
            v_quote ttcustomerasn.abs_cust_part v_quote v_space                   /* scx_ref.scx_custref */
            v_quote ttcustomerasn.abs_model_yr v_quote v_space                    /* scx_ref.scx_modelyr */
            .
      put stream f_cim unformatted
            v_quote ttcustomerasn.abs_order v_quote v_space                       /* sales order */
            v_quote ttcustomerasn.abs_line v_quote v_space   skip                 /* Line */
            v_quote ttcustomerasn.abs_qty v_quote v_space                         /* quantity */
            v_quote ttcustomerasn.abs_um v_quote v_space                          /* um */
            v_quote ttcustomerasn.abs_conv v_quote v_space                        /* conv */
            v_quote ttcustomerasn.abs_site  v_quote v_space                       /*  Site */
            v_quote ttcustomerasn.abs_loc   v_quote v_space                       /*  location */
            /*
            v_quote ttcustomerasn.abs_lot   v_quote v_space                       /*  lot  */
            v_quote ttcustomerasn.abs_ref   v_quote v_space                       /*  ref */
            
            */
            v_default v_space                                                     /* lot */
            v_default v_space                                                     /* ref */
            "N" v_space                                                           /* multi_entry */
            "N" v_space                                                           /* comment */
            skip  .                                                              
                       
	 
	   
   if last-of(ttcustomerasn.abs_asn_nbr) then do:
    put stream f_cim  unformatted   "."  skip  
        skip   .                              
                                
                                
    output stream f_cim close.
    
         
     /* cim load process */
     v_runproagram = "rcshmt.p".
     execname = "xxcustomerasnapi.p".

     
       put unformatted v_cim " " skip
                       v_log " " skip 
                       m_outpath " " skip
                       m_archpath " " skip
                       v_runproagram " " skip
                       v_errstring " " skip 
                       v_errorstatus skip .
                       
       run cimloadprocess(input v_cim,
                          input v_log,
                          input v_runproagram,
                          input vc_traceid,
                          input ttcustomerasn.abs_dataid,
                          input ttcustomerasn.abs_asn_nbr,
                          output v_errstring,
                          output v_errorstatus) .
                          
       v_toterrorstatus = v_toterrorstatus or v_errorstatus.    


            /* Handle cim and log file */
        do vi_filecount = 1 to 2:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
          when 1 then
          assign vc_filename = v_cim
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath.
          when 2 then
          assign vc_filename = v_log
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath.
           end case.           
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).                 
                 
       end. /* do vi_filecount = 1 to 2: */   
       
          assign   ttcustomerasn.abs_cimfile = replace(v_cim,m_workpath,vc_topath)
                   ttcustomerasn.abs_logfile = replace(v_log,m_workpath,vc_topath).
       
       
       
     end. /* if last-of */
     
    end. /* for each ttptp  */

     if  v_toterrorstatus then 

       undo cimloadprocess, leave cimloadprocess.          
            
        
    end.    /* do transaction on error undo cimloadprocess, leave cimloadprocess: */

       if can-find(first api_status where api_status.return_status = "FAILED" ) then
       do:
       
       for each api_status where api_status.return_status = "SUCCESS".
         
         delete api_status.
         
       end.
       
       end.  /* if can-find(first api_status */
         
       

   end. /* if vc_import */
   
   
   
   
       for each ttcustomerasn break by ttcustomerasn.abs_asn_nbr .
       if last-of(ttcustomerasn.abs_asn_nbr) then 
       do:
       vi_reccount = vi_reccount + 1.
              
       run CreateAPILogMaster(input vc_traceid,
                              input "createcustomershipper",
                              input "xxcustomershippermtapi.p",
                              input ttcustomerasn.abs_cimfile,
                              input ttcustomerasn.abs_logfile,
                              input vc_inputjson, 
                              input ttcustomerasn.abs_asn_nbr,
                              input "",
                              input "xxapicustasndata_det",
                              input vi_reccount).  
                              
       end.
        
        
        create xxapicustomerasndata_det.
        assign xxapicustomerasndata_customer              = ttcustomerasn.abs_customer          
               xxapicustomerasndata_asn_nbr               = ttcustomerasn.abs_asn_nbr           
               xxapicustomerasndata_site                  = ttcustomerasn.abs_site              
               xxapicustomerasndata_shipto                = ttcustomerasn.abs_shipto            
               xxapicustomerasndata_invmovment            = ttcustomerasn.abs_inv_movment_code  
               xxapicustomerasndata_shipfrom              = ttcustomerasn.abs_shipfrom          
               xxapicustomerasndata_loc                   = ttcustomerasn.abs_loc               
               xxapicustomerasndata_ship_date             = ttcustomerasn.abs_ship_date         
               xxapicustomerasndata_arrive_date           = ttcustomerasn.abs_arrive_date       
               xxapicustomerasndata_due_date              = ttcustomerasn.abs_due_date          
               xxapicustomerasndata_part                  = ttcustomerasn.abs_part              
               xxapicustomerasndata_cust_part             = ttcustomerasn.abs_cust_part         
               xxapicustomerasndata_po_nbr                = ttcustomerasn.abs_po_nbr            
               xxapicustomerasndata_model_yr              = ttcustomerasn.abs_model_yr          
               xxapicustomerasndata_order                 = ttcustomerasn.abs_order             
               xxapicustomerasndata_line                  = ttcustomerasn.abs_line              
               xxapicustomerasndata_qty                   = ttcustomerasn.abs_qty               
               xxapicustomerasndata_um                    = ttcustomerasn.abs_um                
               xxapicustomerasndata_conv                  = string(ttcustomerasn.abs_conv)             
               xxapicustomerasndata_traceid               = ttcustomerasn.abs_traceid           
               xxapicustomerasndata_dataid                = ttcustomerasn.abs_dataid            
               xxapicustomerasndata_cimfile               = ttcustomerasn.abs_cimfile           
               xxapicustomerasndata_logfile               = ttcustomerasn.abs_logfile           
               xxapicustomerasndata_domain                = global_domain 
               xxapicustomerasndata_field1          = ""
               xxapicustomerasndata_field2          = ""
               xxapicustomerasndata_field3          = ""
               xxapicustomerasndata_field4          = ""
               xxapicustomerasndata_field5          = ""
               xxapicustomerasndata_field6          = ""
                  .
       
       
       
       
       end. /*  for each ttcustomerasn break by ttcustomerasn.abs_asn_nbr . */
   
   
   
   
   
   
   
        /* Handle cimload related files */
         /* Create output json */                   
         run writejsonfile(input hresult,
                  input vc_outputjson,
                  input "FILE",
                  input True,
                  input "UTF-8",
                  output vl_RetOK,
                  output vc_errorstring).
                  
         put unformatted v_cim " " skip
                         v_log " " skip 
                         m_outpath " " skip
                         m_archpath " " skip
                         v_runproagram " " skip
                         v_errstring " " skip 
                         v_errorstatus skip .
       do vi_filecount = 1 to 5:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
      
          when 1 then
          assign vc_filename = replace(vc_inputjson,".json",".p")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 2 then
          assign vc_filename = vc_inputjson
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 3 then
          assign vc_filename = replace(vc_inputjson,".json",".dat")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 4 then
          assign vc_filename = replace(vc_inputjson,".json",".run.log")
                 vc_topath   = m_outpath.
          when 5 then
          assign vc_filename = replace(vc_inputjson,".json",".run.out.json")
                 vc_topath   =  m_outpath.
          end case.
          
         if vl_RetOK then 
         put unformatted vc_filename " " skip
         vc_topath " " skip 
         vi_filecount skip.
     
        
          
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).                 
                   
          
     
       end.       /* do vi_filecount = 1 to 5: */        
       
       output close.   
   
   
   leave.
end. /* repeat on error undo mainloop, retry mainloop: */


PROCEDURE findSchedOrders:
   define input  parameter domain      as character no-undo.
   define input  parameter shipto      as character no-undo.
   define input  parameter shipfrom    as character no-undo.
   define input  parameter shipto_id   as character no-undo.
   define output parameter found_sched as logical   no-undo.
 
   find first scx_ref
      where scx_domain = global_domain
      and   scx_type = 1
      and   scx_shipto   = shipto
      and   scx_shipfrom = shipfrom
      use-index scx_shipfrom
   no-lock no-error.
 
   if not available scx_ref then
      find first scx_ref
         where scx_domain = global_domain
         and   scx_type = 1
         and   scx_shipto   = shipto_id
         and   scx_shipfrom = shipfrom
         use-index scx_shipfrom
      no-lock no-error.
 
   found_sched = available scx_ref.
 
 
END PROCEDURE.   /* findSchedOrders */
